import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import user.bean.User;
import user.dao.UserMapper;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "/user/list")
public class Aaa extends HttpServlet {
    SqlSessionFactory sqlSessionFactory;
    @Override
    public void init() throws ServletException {

        ServletContext servletContext=getServletConfig().getServletContext();
        System.out.println("111");
        servletContext.setAttribute("name","aaa");

        try {
            sqlSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("my_batis.xml"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //这里要做的是就是访问对应的数据库的对应表，然后返回数据，先来安装依赖吧
        SqlSession sqlSession=sqlSessionFactory.openSession();

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

        List<User> users=userMapper.getUsersUseLimit(9,10);
        users.forEach(System.out::println);
    }
}
